/*
 * myrfm12.h
 *
 *  Created on: 24.03.2010
 *      Author: mydani
 */

#include "myrfm12conf.h"
#include <util/crc16.h>

#ifndef MYRFM12_H_
#define MYRFM12_H_

/*! \mainpage MYRFM12 - a well documented, efficient RFM12 library
 *
 * \section intro_sec Introduction
 *
 * This is the documentation for MYRFM12. MYRFM12 provides simple APIs to 
 * easily use your rfm12 modules with your microcontrollers.
 *
 * \section usage_sec Usage
 *  
 * Copy the library into your project, add the include directory to your 
 * makefile, adapt the files myrfm12conf.h and myrfm12conf.c
 * to your specific HW configuration, then make, flash, enjoy. :)
 */



/*! \file myrfm12.h
 *  This file has to be included (and only this file!) to get access to the
 *  provided API. This file should not be modified! */



/*! \defgroup APIFUN API Functions */
/*@{*/


	/*! \defgroup API_SETUP myrfm12 function: myrfm12_setup */
	/*@{*/

		/* API FUNCTION: myrfm12_setup */

		/*!\brief  myrfm12_setup shall be called to completely setup the rfm12,
		 * which includes a call to myrfm12_spi_init to setup the communication. */
		void myrfm12_setup(void);

	/*@}*/


	/*! \defgroup API_TRANS myrfm12 function: myrfm12_trans */
	/*@{*/

		/* API FUNCTION: myrfm12_trans */

		/*!\brief  myrfm12_trans is used to send the usual 2-byte commands to the
		 *         rfm12 module.
		 *\param  cmd   Command which shall be send to the rfm12 module */
		uint16_t myrfm12_trans(uint16_t cmd);

	/*@}*/


	/*! \defgroup API_READY myrfm12 function: myrfm12_ready */
	/*@{*/

		/* API FUNCTION: myrfm12_ready */

		  /*!\brief		myrfm12_ready waits until the rfm12 module is able to receive new data via SPI */
		  void myrfm12_ready(void);

	/*@}*/


	/*! \defgroup CMD_CFG RFM12 Command: Configuration Setting */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_config */

		#define MYRFM12_CMD__CONFIG__TXFIFO_ON				0x0080

		#define MYRFM12_CMD__CONFIG__RXFIFO_ON				0x0040

		#define MYRFM12_CMD__CONFIG__BAND_315MHZ			0x0000
		#define MYRFM12_CMD__CONFIG__BAND_433MHZ			0x0010
		#define MYRFM12_CMD__CONFIG__BAND_868MHZ			0x0020
		#define MYRFM12_CMD__CONFIG__BAND_915MHZ			0x0030

		#define MYRFM12_CMD__CONFIG__CAPACITOR8_5			0x0000
		#define MYRFM12_CMD__CONFIG__CAPACITOR9				0x0001
		#define MYRFM12_CMD__CONFIG__CAPACITOR9_5			0x0002
		#define MYRFM12_CMD__CONFIG__CAPACITOR10			0x0003
		#define MYRFM12_CMD__CONFIG__CAPACITOR10_5			0x0004
		#define MYRFM12_CMD__CONFIG__CAPACITOR11			0x0005
		#define MYRFM12_CMD__CONFIG__CAPACITOR11_5			0x0006
		#define MYRFM12_CMD__CONFIG__CAPACITOR12			0x0007
		#define MYRFM12_CMD__CONFIG__CAPACITOR12_5			0x0008
		#define MYRFM12_CMD__CONFIG__CAPACITOR13			0x0009
		#define MYRFM12_CMD__CONFIG__CAPACITOR13_5			0x000A
		#define MYRFM12_CMD__CONFIG__CAPACITOR14			0x000B
		#define MYRFM12_CMD__CONFIG__CAPACITOR14_5			0x000C
		#define MYRFM12_CMD__CONFIG__CAPACITOR15			0x000D
		#define MYRFM12_CMD__CONFIG__CAPACITOR15_5			0x000E
		#define MYRFM12_CMD__CONFIG__CAPACITOR16			0x000F

		#if (MYRFM12_CONF_SETUP_CAPACITOR < MYRFM12_CMD__CONFIG__CAPACITOR8_5) || (MYRFM12_CONF_SETUP_CAPACITOR > MYRFM12_CMD__CONFIG__CAPACITOR16)
			#error MYRFM12_CONF_SETUP_CAPACITOR IS OUT OF BOUNDS.
		#endif

		/*!\brief myrfm12_cmd_config is used to configure the basic settings
		 * of the rfm12 module, like enabling/disabling TX and RX FIFO,
		 * adjusting base frequency and changing the internal clock by
		 * adaptation of a capacitor.
		 *\param configbyte This byte is a bitmask. Each bit has a
		 *       corresponding define, starting with MYRFM12_CMD__CONFIG__... */
		#define myrfm12_cmd_config(configbyte)	myrfm12_trans(0x8000 | configbyte)



	/*@}*/


	/*! \defgroup CMD_PWRMGMNT RFM12 Command: Power Management */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_powermanagement */

		/*!\brief If this bit is set in the call to myrfm12_cmd_powermanagement(...),
		* it will enable the receiver */
		#define MYRFM12_CMD__POWER__ENABLERECEIVER		1 << 7
		/*!\brief If this bit is set in the call to myrfm12_cmd_powermanagement(...),
		* it will enable the ebb */
		#define MYRFM12_CMD__POWER__ENABLEBB				1 << 6
		/*!\brief If this bit is set in the call to myrfm12_cmd_powermanagement(...),
		* it will enable the transmitter */
		#define MYRFM12_CMD__POWER__ENABLETRANSMITTER	1 << 5
		/*!\brief If this bit is set in the call to myrfm12_cmd_powermanagement(...),
		* it will enable the synthesizer*/
		#define MYRFM12_CMD__POWER__ENABLESYNTHESIZER	1 << 4
		/*!\brief If this bit is set in the call to myrfm12_cmd_powermanagement(...),
		* it will enable the crystal oscillator */
		#define MYRFM12_CMD__POWER__ENABLEOSCILLATOR		1 << 3
		/*!\brief If this bit is set in the call to myrfm12_cmd_powermanagement(...),
		* it will enable the brown out detection */
		#define MYRFM12_CMD__POWER__ENABLEBROWNOUT		1 << 2
		/*!\brief If this bit is set in the call to myrfm12_cmd_powermanagement(...),
		* it will enable the wakeup timer */
		#define MYRFM12_CMD__POWER__ENABLEWAKEUPTIMER	1 << 1
		/*!\brief If this bit is set in the call to myrfm12_cmd_powermanagement(...),
		* it will disable the system clock output at the CLK pin */
		#define MYRFM12_CMD__POWER__DISABLECLOCKOUTPUT	1 << 0

		/*!\brief myrfm12_cmd_powermanagement enables/disables defined functions on
		* the rfm12 module, which can be used to decrease the necessary amount
		* of power - e.g. when using a battery as power supply.
		*\param powermanagementbyte This byte is a bitmask, which consists of 8 bits
		*       which are turn defined functions on or off. Each bit has a
		*       corresponding define, starting with MYRFM12_PMB_... */
		#define myrfm12_cmd_powermanagement(powermanagementbyte) myrfm12_trans(0x8200 | powermanagementbyte)

		/*!\brief myrfm12_sleep will set the rfm12_module to sleep mode */
		#define myrfm12_sleep()	myrfm12_cmd_powermanagement(MYRFM12_CMD__POWER__DISABLECLOCKOUTPUT)

		/*!\brief myrfm12_wakeup will wakupe the rfm12 module,enable rx and tx*/
		#define myrfm12_wakeup()	myrfm12_cmd_powermanagement(MYRFM12_CMD__POWER__ENABLERECEIVER|MYRFM12_CMD__POWER__ENABLETRANSMITTER)

	/*@}*/


	/*! \defgroup CMD_FREQSET RFM12 Command: Frequency Setting */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_frequencysetting */
		#define MYRFM12_CMD__FREQ__310MHZ_ADD0_0025MHZ(A)	(((A-3100000)/25)&0xFFF)
		#define MYRFM12_CMD__FREQ__430MHZ_ADD0_0025MHZ(A)	(((A-4300000)/25)&0xFFF)
		#define MYRFM12_CMD__FREQ__860MHZ_ADD0_0050MHZ(A)	(((A-8600000)/50)&0xFFF)
		#define MYRFM12_CMD__FREQ__900MHZ_ADD0_0075MHZ(A)	(((A-9000000)/75)&0xFFF)

		#if (((MYRFM12_CONF_SETUP_FREQUENCY) >= 3100900) && ((MYRFM12_CONF_SETUP_FREQUENCY) <= 3197575))
			#define MYRFM12_CONF_SETUP_BASEFREQ	MYRFM12_CMD__CONFIG__BAND_315MHZ
			#define MYRFM12_CONF_SETUP_ADDFREQ	MYRFM12_CMD__FREQ__310MHZ_ADD0_0025MHZ(MYRFM12_CONF_SETUP_FREQUENCY)
		#elif ((MYRFM12_CONF_SETUP_FREQUENCY >= 4300900) && (MYRFM12_CONF_SETUP_FREQUENCY <= 4397575))
			#define MYRFM12_CONF_SETUP_BASEFREQ	MYRFM12_CMD__CONFIG__BAND_433MHZ
			#define MYRFM12_CONF_SETUP_ADDFREQ	MYRFM12_CMD__FREQ__430MHZ_ADD0_0025MHZ(MYRFM12_CONF_SETUP_FREQUENCY)
		#elif ((MYRFM12_CONF_SETUP_FREQUENCY >= 8601800) && (MYRFM12_CONF_SETUP_FREQUENCY <= 8795150))
			#define MYRFM12_CONF_SETUP_BASEFREQ	MYRFM12_CMD__CONFIG__BAND_868MHZ
			#define MYRFM12_CONF_SETUP_ADDFREQ	MYRFM12_CMD__FREQ__860MHZ_ADD0_0050MHZ(MYRFM12_CONF_SETUP_FREQUENCY)
		#elif ((MYRFM12_CONF_SETUP_FREQUENCY >= 9002700) && (MYRFM12_CONF_SETUP_FREQUENCY <= 9292725))
			#define MYRFM12_CONF_SETUP_BASEFREQ	MYRFM12_CMD__CONFIG__BAND_915MHZ
			#define MYRFM12_CONF_SETUP_ADDFREQ	MYRFM12_CMD__FREQ__900MHZ_ADD0_0075MHZ(MYRFM12_CONF_SETUP_FREQUENCY)
		#else
			#error MYRFM12_CONF_SETUP_FREQUENCY IS OUT OF BOUNDS.
		#endif

		/*!\brief myrfm12_cmd_frequencysetting will set the frequency the rfm12
		 * module will receive and transmit on.
		 * \param frequency as addition to the base frequency. Use the defines
		 * MYRFM12_CMD__FREQ__<band>_... as an argument.
		 * Example: MYRFM12_CMD__FREQ__310MHZ_ADD0_0025MHZ(4380000) as argument
		 * sets the frequency to 438.00MHz (if the base frequency is set to
		 * 430Mhz!).*/
		#define myrfm12_cmd_frequencysetting(frequency)		myrfm12_trans(0xA000|frequency)

	/*@}*/


	/*! \defgroup CMD_DATARATE RFM12 Command: Datarate Setting */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_datarate */

		#define MYRFM12_MIN_BAUD_RATE_WITHOUT_PRESCALER  (10000000/29/(128+1))
		#define MYRFM12_MAX_BAUD_RATE_WITHOUT_PRESCALER  (10000000/29)
		#define MYRFM12_MIN_BAUD_RATE_WITH_PRESCALER     (10000000/29/(128+1)/8)
		#define MYRFM12_MAX_BAUD_RATE_WITH_PRESCALER     ((10000000/29/8))

		#define MYRFM12_CALC_R_WITHOUT_PRESCALER(baud)   (((10000000/29/1)/baud)-1)
		#define MYRFM12_CALC_R_WITH_PRESCALER(baud)      (((10000000/29/8)/baud)-1)

		#if (MYRFM12_CONF_SETUP_BAUDRATE < MYRFM12_MIN_BAUD_RATE_WITH_PRESCALER) || (MYRFM12_CONF_SETUP_BAUDRATE > MYRFM12_MAX_BAUD_RATE_WITHOUT_PRESCALER)
			#error MYRFM12_CONF_SETUP_BAUDRATE IS OUT OF BOUNDS.
		#endif

		/*!\brief myrfm12_cmd_datarate sets the baudrate to be used by the rfm12
		* \param baud baudrate which the rfm12 module shall send with. Minimum is
		* 334 baud, maximum is 344827 baud. The function will not check limits to
		* reduce ROM usage. */
		void myrfm12_cmd_datarate(uint16_t baud);

	/*@}*/


	/*! \defgroup CMD_MODPWR RFM12 Command: Modulation and Power Setting */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_modulation_power */

		#define MYRFM12_CMD__MODPWR__MAX_POWER	7
		#define MYRFM12_CMD__MODPWR__MIN_POWER	0

		#if (MYRFM12_CONF_SETUP_POWER > MYRFM12_CMD__MODPWR__MAX_POWER) || (MYRFM12_CONF_SETUP_POWER < MYRFM12_CMD__MODPWR__MIN_POWER)
		   #error MYRFM12_CONF_SETUP_POWER IS OUT OF BOUNDS.
		#endif

		#define MYRFM12_MAX_BANDWITH 15
		#define MYRFM12_MIN_BANDWITH 0

		#if (MYRFM12_CONF_SETUP_BANDWITH > MYRFM12_MAX_BANDWITH) || (MYRFM12_CONF_SETUP_BANDWITH < MYRFM12_MIN_BANDWITH)
			#error MYRFM12_CONF_SETUP_BANDWITH IS OUT OF BOUNDS.
		#endif

		#define MYRFM12_MAX_MODULATIONPOLARITY 1
		#define MYRFM12_MIN_MODULATIONPOLARITY 0

		#if (MYRFM12_CONF_SETUP_MODULATIONPOLARITY > MYRFM12_MAX_MODULATIONPOLARITY) || (MYRFM12_CONF_SETUP_MODULATIONPOLARITY < MYRFM12_MIN_MODULATIONPOLARITY)
			#error MYRFM12_CONF_SETUP_MODULATIONPOLARITY IS OUT OF BOUNDS!
		#endif

		/*!\brief myrfm12_cmd_modulation_power sets the power of the transmission,
		 * the frequency deviation and the modulation polarity.
		 * \param power power, 0..7
		 * \param freqdev Frequency deviation, 0..15
		 * \param modulationpolarity Polarity of the modulation, 0..1 */
		#define myrfm12_cmd_modulation_power(power,freqdev,modulationpolarity) myrfm12_trans(0x9800 | (7-(0x7 & power)) | ((0x0F & (uint16_t)freqdev)<<4) | ((0x1 & modulationpolarity)<< 8) )

	/*@}*/


	/*! \defgroup CMD_RXC RFM12 Command: Receiver Control */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_rxcontrol */

		/*!\brief PIN 20 acts as interrupt input */
		#define MYRFM12_CMD__RXCTRL__PIN20_EXTINTIN			0x0000
		/*!\brief PIN 20 acts VDI (valid data indicator) output */
		#define MYRFM12_CMD__RXCTRL__PIN20_VDIOUT			0x0400

		/*!\brief Receiver bandwith which the receiver should receive in */
		#define MYRFM12_CMD__RXCTRL__BASEBANDWITH_400KHZ 	0x0020
		#define MYRFM12_CMD__RXCTRL__BASEBANDWITH_340KHZ 	0x0040
		#define MYRFM12_CMD__RXCTRL__BASEBANDWITH_270KHZ 	0x0060
		#define MYRFM12_CMD__RXCTRL__BASEBANDWITH_200KHZ 	0x0080
		#define MYRFM12_CMD__RXCTRL__BASEBANDWITH_134KHZ 	0x00A0
		#define MYRFM12_CMD__RXCTRL__BASEBANDWITH_67KHZ 	0x00C0

		/*!\brief VDI will be set if CR_LOCK or DQD */
		#define MYRFM12_CMD__RXCTRL__VDIRESPTIME_FAST	 	0x0000
		/*!\brief VDI will be set if  CR_LOCK AND (DQD or DRSSI)*/
		#define MYRFM12_CMD__RXCTRL__VDIRESPTIME_MEDIUM		0x0100
		/*!\brief VDI will be set if R/S(DRSSI  OR DQD OR CR_LOCK) AND
		 * (DRSSI AND DQD AND CR_LOCK)*/
		#define MYRFM12_CMD__RXCTRL__VDIRESPTIME_SLOW	 	0x0200
		/*!\brief VDI will be set to on always */
		#define MYRFM12_CMD__RXCTRL__VDIRESPTIME_ALWAYS 	0x0300

		/*!\brief LNA (low noise amplifier) gain, 0DB means most gain */
		#define MYRFM12_CMD__RXCTRL__LNAGAIN_0DB		 	0x0000
		/*!\brief LNA (low noise amplifier) gain reduced by 6db */
		#define MYRFM12_CMD__RXCTRL__LNAGAIN_MIN6DB			0x0008
		/*!\brief LNA (low noise amplifier) gain reduced by 14db */
		#define MYRFM12_CMD__RXCTRL__LNAGAIN_MIN14DB	 	0x0010
		/*!\brief LNA (low noise amplifier) gain reduced by 20db */
		#define MYRFM12_CMD__RXCTRL__LNAGAIN_MIN20DB		0x0018

		/*!\brief DRSSI digital received signal strength information,
		 * the minimum necessary strenth of the signal to receive it.
		 * -103db means even the lowest signals can be received. */
		#define MYRFM12_CMD__RXCTRL__DRSSI_M103DB			0x0000
		#define MYRFM12_CMD__RXCTRL__DRSSI_M97DB			0x0001
		#define MYRFM12_CMD__RXCTRL__DRSSI_M91DB			0x0002
		#define MYRFM12_CMD__RXCTRL__DRSSI_M85DB			0x0003
		#define MYRFM12_CMD__RXCTRL__DRSSI_M79DB			0x0004
		#define MYRFM12_CMD__RXCTRL__DRSSI_M73DB			0x0005
		#define MYRFM12_CMD__RXCTRL__DRSSI_M67DB			0x0006
		#define MYRFM12_CMD__RXCTRL__DRSSI_M61DB			0x0007

		/*!\brief myrfm12_cmd_rxcontrol configures the rx part of the rfm12
		 * module.
		 * \param rxcontrolbyte Use the defines starting with
		 * MYRFM12_CMD__RXCTRL__... to setup the receiver. */
		#define myrfm12_cmd_rxcontrol(rxcontrolbyte) myrfm12_trans(0x9000 | rxcontrolbyte)

	/*@}*/


	/*! \defgroup CMD_DF RFM12 Command: Data Filter */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_datafilter */

		/*!\brief If defined the baud rate generator will change to the detected
		 * baud rate (slower one) - if detected. */
		#define MYRFM12_CMD__DF__CLKRECAUTOLOCK_ENABLE		0x0080
		/*!\brief This enables the baud rate generator fast mode */
		#define MYRFM12_CMD__DF__CLKRECFASTMODE_ENABLE		0x0040

		/*!\brief If defined an analog data filter is expected (additional
		 * hardware. */
		#define MYRFM12_CMD__DF__FILTERTYPE_OOK				0x0010
		/*!\brief If defined a digital data filter (onbard) will be used. */
		#define MYRFM12_CMD__DF__FILTERTYPE_DIGITAL			0x0000

		/*!\brief DQD - data quality detection threshold. Configures a threshold
		 * based on the number of spikes of the received unfiltered data, which
		 * is then used to decide about the quality of the incoming signal.
		 * The DQD is a 3bit value. */
		#define MYRFM12_CMD__DF__DQDTHRESH(A)				(A&0x7)

		/*!\brief myrfm12_cmd_datafilter configures the data filter of the rfm12
		 * module. Use the defines starting with
		 * MYRFM12_CMD__DF__... to setup the data filter. */
		#define myrfm12_cmd_datafilter(datafilterbyte) myrfm12_trans(0xC228 | datafilterbyte)

	/*@}*/


	/*! \defgroup CMD_FIFO RFM12 Command: Fifo */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_fifo */

		/*!\brief Defines the FIFO interrupt level */
		#define MYRFM12_CMD__FIFO__FIFOIRLEVEL(A)	((uint8_t)((A&0xF) << 4))

		/*!\brief If set the FIFO will only be filled when a SYNC byte was
		 * received previously. */
		#define MYRFM12_CMD__FIFO__FIFOFILL_SYNC		0x0004
		/*!\brief If set the FIFO will always be filled. */
		#define MYRFM12_CMD__FIFO__FIFOFILL_ALWAYS		0x0000

		/*!\brief Enables the FIFO filling */
		#define MYRFM12_CMD__FIFO__FIFO_FILL			0x0002

		/*!\brief enables high sensitivity reset mode (system reset when 200mV
		 * change on power supply voltage is detected). */
		#define MYRFM12_CMD__FIFO__HSRESETMODE_ENABLE	0x0001
		/*!\brief disables high sensitivity reset mode. */
		#define MYRFM12_CMD__FIFO__HSRESETMODE_DISABLE	0x0000

		/*!\brief Configures the fifo behaviour of the rfm12 module.
		 * \param fifobyte Use the defines starting with MYRFM12_CMD__FIFO__. */
		#define myrfm12_cmd_fifo(fifobyte) myrfm12_trans(0xCA00 | fifobyte)

	/*@}*/


	/*! \defgroup CMD_READ RFM12 Command: Read Fifo */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_readfifo */

		/*!\brief Reads the fifo into a uint16_t.
		 * \param readfifobyte variable into which the fifo will be read. */
		#define myrfm12_cmd_readfifo(readfifobyte) readfifobyte = myrfm12_trans(0xB000)

	/*@}*/


	/*! \defgroup CMD_AFC RFM12 Command: AFC Automatic Frequency Control */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_afc */

		/*!\brief Automatically enable AFC. */
		#define MYRFM12_CMD__AFC__AUTOMODE_MCU				0
		/*!\brief Enable AFC once at power on. */
		#define MYRFM12_CMD__AFC__AUTOMODE_ONCEPOWERON		0x0040
		/*!\brief Enable AFC when VDI is HIGH.*/
		#define MYRFM12_CMD__AFC__AUTOMODE_OFFSVDIHIGH		0x0080
		/*!\brief AFC independent of VDI.*/
		#define MYRFM12_CMD__AFC__AUTOMODE_OFFSETKEEP		0x00C0

		/*!\brief Range Limit - no restriction */
		#define MYRFM12_CMD__AFC__RANGE_NORESTRICTION		0
		/*!\brief Range Limit - -16..15 */
		#define MYRFM12_CMD__AFC__RANGE_WIDE				0x0010
		/*!\brief Range Limit - -8..7 */
		#define MYRFM12_CMD__AFC__RANGE_MIDDLE				0x0020
		/*!\brief Range Limit - -4..3 */
		#define MYRFM12_CMD__AFC__RANGE_NARROW				0x0030

		/*!\brief If set the calculated offset value is stored. */
		#define MYRFM12_CMD__AFC__STOREOFFSETINOUTREG		0x0008

		/*!\brief If set high accuracy (slower) calculation is enabled. */
		#define MYRFM12_CMD__AFC__ENABLEHIGHACCURACY		0x0004

		/*!\brief Release AFC offset calculation */
		#define MYRFM12_CMD__AFC__ENABLEOUTREG				0x0002

		/*!\brief Enable AFC calculation */
		#define MYRFM12_CMD__AFC__ENABLEAFC				0x0001

		/*!\brief Configures the AFC.
		 * \param afcbyte Use the defines starting with MYRFM12_CMD__AFC__. */
		#define myrfm12_cmd_afc(afcbyte) myrfm12_trans(0xC400 | afcbyte)

	/*@}*/


	/*! \defgroup CMD_WRITE RFM12 Command: Transfer Register Write */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_txbyte */

		/*!\brief Transfer a byte into the tx register.
		 * \param txbyte Byte which is transfered to the tx register. */
		#define myrfm12_cmd_txbyte(txbyte) myrfm12_trans(0xB800 | txbyte)

	/*@}*/


	/*! \defgroup CMD_WAKE RFM12 Command: Wakeup Timer */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_wakeuptimer */

		#define MYRFM12_CMD__WAKEUP__EXPONENT(A)	((0x1F&A)<<8)
		#define MYRFM12_CMD__WAKEUP__BASE(A)		((0xFF&A))

		/*!\brief Configures the wakeup time.
		 * \param wakeupbyte Use the defines starting with MYRFM12_CMD__WAKEUP__
		 * as arguments. Time calculates as BASE * 2^EXPONENT in milliseconds.*/
		#define myrfm12_cmd_wakeuptimer(wakeupbyte) myrfm12_trans(0xE000 | wakeupbyte)

	/*@}*/


	/*! \defgroup CMD_LD RFM12 Command: Low Duty Cycle */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_lowdutycycle */

		#define MYRFM12_CMD__LOWDUTY__DUTY(A)				((A&0x7F)<<1)
		#define MYRFM12_CMD__LOWDUTY__ENABLE_DUTY			0x0001

		/*!\brief Configures the low duty cycle (maximum transfer time per
		 * hour.
		 * \param ldbyte Use the defines starting with MYRFM12_CMD__LOWDUTY__
		 * as arguments. (DUTY * 2) +1 / M * 100*/
		#define myrfm12_cmd_lowdutycycle(ldbyte) myrfm12_trans(0xC800 | ldbyte)

	/*@}*/


	/*! \defgroup CMD_LOWBAT RFM12 Command: Low Battery And Clock Divider */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_lowbatandclockdivider */

		#define MYRFM12_CMD__LOWBATCLOCK__CLOCK_1MHZ	0<<5
		#define MYRFM12_CMD__LOWBATCLOCK__CLOCK_5MHZ	1<<5
		#define MYRFM12_CMD__LOWBATCLOCK__CLOCK_3D33MHZ	2<<5
		#define MYRFM12_CMD__LOWBATCLOCK__CLOCK_2D5MHZ	3<<5
		#define MYRFM12_CMD__LOWBATCLOCK__CLOCK_2MHZ	4<<5
		#define MYRFM12_CMD__LOWBATCLOCK__CLOCK_1D66MHZ	5<<5
		#define MYRFM12_CMD__LOWBATCLOCK__CLOCK_1D25MHZ	6<<5
		#define MYRFM12_CMD__LOWBATCLOCK__CLOCK_10MHZ	7<<5

		/*!\brief Sets the low battery voltage threshold. Minimum is 2V, and
		 * it increases per 1 by 0.2V. */
		#define MYRFM12_CMD__LOWBATCLOCK__LOWBATVOLT(A)		(0x1F&A)

		/*!\brief Configures the low battery detection and the clock divider.
		 * \param lowbatclockbyte Use the defines starting with MYRFM12_CMD__LOWDUTY__
		 * as arguments. (DUTY * 2) +1 / M * 100*/
		#define myrfm12_cmd_lowbatandclockdivider(lowbatclockbyte) myrfm12_trans(0xC000 | lowbatclockbyte)

	/*@}*/


	/*! \defgroup CMD_STATUS RFM12 Command: Read Status Byte */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_readstatus */

//		#define MYRFM12_CMD__READSTATUS__FFIT_RGIT	1 << 18

		/*!\brief Reads the status into a uint16_t.
		 * \param readstatusbyte variable into which the status byte is read. */
		#define myrfm12_cmd_readstatus(readstatusbyte) readstatusbyte = myrfm12_trans(0x0000)

	/*@}*/


	/*! \defgroup CMD_PLL RFM12 Command: PLL Setting */
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_pll */

		#define MYRFM12_CMD__PLL__CLOCK_5_OR_10_MHZ			0<<5
		#define MYRFM12_CMD__PLL__CLOCK_3_33_MHZ			1<<5
		#define MYRFM12_CMD__PLL__CLOCK_2_5_MHZ_OR_BELOW	2<<5

		#define MYRFM12_CMD__PLL__CRYSTALOSCILLATOR_LOWPOWERMODE_ENABLED	1<<4

		#define MYRFM12_CMD__PLL__DDY	1<<3

		#define MYRFM12_CMD__PLL__DDI	0<<2

		#define MYRFM12_CMD__PLL__BANDWIDTH_86KBPS	0
		#define MYRFM12_CMD__PLL__BANDWIDTH_256KBPS	1

		/*!\brief Configures the PLL of the rfm12 module.
		 * \param pllbyte Use the defines starting with MYRFM12_CMD__PLL__ */
		#define myrfm12_cmd_pll(pllbyte) myrfm12_trans(pllbyte)

	/*@}*/


	/*! \defgroup CMD_SYNC RFM12 Command: Write Sync Byte*/
	/*@{*/

		/* API FUNCTION: myrfm12_cmd_writesync */

		/*!\brief Transfer the sync byte to the register.
		 * \param syncbyte Byte which is used to synchronize. */
		#define myrfm12_cmd_writesync(syncbyte) myrfm12_trans(0xCE00 | syncbyte)

	/*@}*/


	/*! \defgroup API_SB myrfm12 function: void myrfm12_sendsyncrobytes() */
	/*@{*/

		/* API FUNCTION: void myrfm12_sendsyncrobytes() */

		/*!\brief  void myrfm12_sendsyncrobytes() is used to send a number of
		 * bytes to synchronize the tx and rx rfm12 modules' clocks. */
		void myrfm12_sendsyncrobytes();

	/*@}*/



	/*! \defgroup API_SSB myrfm12 function: void myrfm12_sendsyncbytes() */
	/*@{*/

		/* API FUNCTION: void myrfm12_sendsyncbytes() */

		/*!\brief  void myrfm12_sendsyncbytes() is used to send the magic 2
		 * bytes which act as a start of transmission marker. */
		void myrfm12_sendsyncbytes();

	/*@}*/



	/*! \defgroup API_TXD myrfm12 function: myrfm12_txdata */
	/*@{*/

		/* API FUNCTION: void myrfm12_txdata() */

		/*!\brief  void myrfm12_txdata() is used to send variable length data
		 * \param *data Pointer to the uint8_t byte[].
		 * \param number Number of bytes to be transfered */
		uint8_t myrfm12_txdata(uint8_t* data, uint8_t number, uint8_t destAddr);
		void myrfm12_txframe(uint8_t* txFrame, uint8_t number);
		void myrfm12_txack(uint8_t ack_nack, uint8_t destAddr);

	/*@}*/



	/*! \defgroup API_RXD myrfm12 function: myrfm12_rxdata */
	/*@{*/

		/* API FUNCTION: void myrfm12_rxdata() */

		/*!\brief  void myrfm12_rxdata() is used to receive variable length data
		 * \param *data Pointer to the uint8_t byte[].
		 * \return number Number of bytes */
		uint8_t myrfm12_rxdata(uint8_t* data);
		uint8_t myrfm12_rxack(uint8_t sourceAddr);

	/*@}*/

		uint16_t calculate_crc16(uint8_t* data, uint8_t number);

/*@}*/

#endif /* MYRFM12_H_ */

